package com.csscis.ssm.core.util;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by qinghe on 2016/3/28.
 */
public class FileUtil {

    private static final String FILE_TYPE_ZIP = ".zip";
    private static final String FILE_TYPE_TMP = ".tmp";
    private static final String FILE_TYPE_PIC = ".png";
    private static final String FILE_TYPE_JPG = ".jpg";

    /**
     * 缓存大小 20K
     */
    private static final int BUFFER_SIZE = 20 * 1024; // 20K

    /**
     * 删除文件下某类文件，keep 为保留文件
     *
     * @param folder
     * @param keep
     */
    public static void deleteFile(String folder, String keep) {
        File dir = new File(folder);

        String[] list = dir.list();
        if (list.length == 0) {
            return;
        }
        File fileDelete;
        for (String filename : list) {
            if (filename.endsWith(FILE_TYPE_JPG)) {
                if (!filename.contains(keep)) {
                    String temp = folder + filename;
                    fileDelete = new File(temp);
                    boolean isdeleted = fileDelete.delete();
                    if (isdeleted) {
                        System.out.println("文件删除成功-- --" + temp);
                    } else {
                        System.out.println("文件删除失败-- --" + temp);
                    }
                }
            }
        }
    }

    /**
     * 删除目录下指定的图片
     *
     * @param folder
     * @param name
     */
    public static void deleteFileByName(String folder, String name) {
        File dir = new File(folder);

        String[] list = dir.list();
        if (null == list || list.length == 0) {
            return;
        }
        File fileDelete;
        for (String filename : list) {
            if (filename.endsWith(FILE_TYPE_PIC)) {
                if (filename.contains(name)) {
                    String temp = folder + filename;
                    fileDelete = new File(temp);
                    boolean isdeleted = fileDelete.delete();
                    if (isdeleted) {
                        System.out.println("文件删除成功-- --" + temp);
                    } else {
                        System.out.println("文件删除失败-- --" + temp);
                    }
                }
            }
        }
    }

    /**
     * 拷贝文件
     *
     * @param src
     * @param dst
     */
    public static void copyFile(File src, File dst) {
        try {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(src),
                        BUFFER_SIZE);
                out = new BufferedOutputStream(new FileOutputStream(dst),
                        BUFFER_SIZE);
                byte[] buffer = new byte[BUFFER_SIZE];
                while (in.read(buffer) > 0) {
                    out.write(buffer);
                }
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String pathNow = "D:\\work\\test\\photo";
        File path = new File(pathNow);
        //列出该目录下所有文件和文件夹
        File[] files = path.listFiles();
        //按照文件最后修改日期倒序排序
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return (int) (file2.lastModified() - file1.lastModified());
            }
        });
        //取出第一个(即最新修改的)文件，打印文件名
        System.out.println(files[0].getName());

        deleteFile(pathNow,files[0].getName());
    }
}
